package com.kafka.service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import net.sf.json.JSONObject;

import org.apache.log4j.PropertyConfigurator;

import com.kafka.bean.Iccard;
import com.kafka.bean.User;
import com.kafka.common.String.StringValues;
import com.kafka.core.BaseProducer;
import com.kafka.dao.IccardMapper;
import com.kafka.dao.UserMapper;
import com.kafka.enumtype.ResponseType;
import com.kafka.util.AES;
import com.kafka.util.BaseUtil;

/**
 * 接口资源
 * 
 * @author 雷洪岸
 * 
 */
@Path("query")
public class QueryResource extends BaseProducer{
	IccardMapper iccardDao = sqlSession.getMapper(IccardMapper.class);
	UserMapper userDao = sqlSession.getMapper(UserMapper.class);
	// 解密密钥
	private static String skey;

	public QueryResource() {
		Properties properties = new Properties();
		try {
			properties.load(ClassLoader
					.getSystemResourceAsStream("sysconfig.properties"));
			skey = properties.getProperty("sKey");
		} catch (IOException e) {
			e.printStackTrace();
		}
		PropertyConfigurator.configure("config/log4j.properties");
	}
	
	@Path("/iccard")
	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public String updateInteface(@Valid String message) {
		// 数据解密
		if (!message.isEmpty()) {
			try {
				message = AES.Decrypt(message, skey);
				log.info("###Decrypt:" + message.toString() + ",time:" + BaseUtil.getDate());
			} catch (Exception e) {
				log.info("###Decrypt:" + "解密数据时发生异常:" + e.toString() + ",time:" + BaseUtil.getDate());
				return failMsg(JSONObject.fromObject(message), "解密数据时发生异常:"+e.toString());
			}
		}
		JSONObject obj = rebuildJSON(JSONObject.fromObject(message));
		JSONObject datas = obj.getJSONObject("datas");
		Map<Object,Object> query = new HashMap<>();//user查询参数
		JSONObject result = new JSONObject();//查询结果
		query.clear();
		query.put("card_type", datas.getString("cardtype"));
		query.put("iccardno", datas.getString("cardno"));
		Iccard iccard = iccardDao.selectByCondition(query);
		//先查询本地数据库是否有该iccard
		if(iccard!=null){
			try {
				//如果本中心有该iccard则封装数据并返回
				query.clear();
				query.put("uuid", iccard.getUuid());
				User user = userDao.selectByCondition(query); 
				if(user!=null){
					String trueName = "";
					if(!user.getTrueName().isEmpty()){
						trueName = user.getTrueName();
					}
					result.put("uuid", iccard.getUuid());
					result.put("cardno", iccard.getIccardno());
					result.put("balance", iccard.getBalance());
					result.put("cardtype", iccard.getCardType());
					result.put("true_name", trueName);
					result.put("integral", 0);
					result.put("status", iccard.getEnabled()); 
					return successMsg(obj.get("msgid"),result);
				}else{
					return failMsg(obj.get("msgid"),StringValues.USER_NOT_EXIST,ResponseType.USER_NOT_EXIST.toString());
				}
			} catch (Exception e) {
				return failMsg(obj.get("msgid"),e.toString(),ResponseType.SYSTEM_ERROR.toString());
			}
		}else{
			//向平台请求查询该iccard号
			JSONObject params = new JSONObject();
			params.put("cardno", datas.getString("cardno"));
			params.put("cardtype", datas.getInt("cardtype"));
			JSONObject cardInfo =queryIccardWithPlatform(params);
			if(cardInfo!=null){
				cardInfo = rebuildJSON(cardInfo);
				//如果平台存在该iccard则返回信息
				result.put("uuid", cardInfo.getString("uuid"));
				result.put("cardno", cardInfo.getString("cardno"));
				result.put("balance", cardInfo.getString("balance"));
				result.put("cardtype", cardInfo.getString("cardtype"));
				result.put("true_name", cardInfo.getString("true_name"));
				result.put("integral", 0);
				result.put("status", cardInfo.getString("status")); 
				return successMsg(obj.get("msgid"),rebuildJSON(result));
			}else{
				//如果不存在则返回错误信息
				return failMsg(obj.get("msgid"),StringValues.ICCARD_NOT_FOUND,ResponseType.ICCARD_NOT_FOUND.toString());
			}
		}
	}
}
